/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: EditFactor2EvaluatePresenter.java
 * Date: 07/02/2011
 */

package com.ingenius.sau.client.presenter;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HasWidgets;
import com.ingenius.sau.client.AppController;
import com.ingenius.sau.client.Constants;
import com.ingenius.sau.client.event.ListFactors2EvaluateEvent;
import com.ingenius.sau.client.service.CoreServiceAsync;
import com.ingenius.sau.client.view.Display;
import com.ingenius.sau.shared.model.core.Factor2Evaluate;
import com.ingenius.sau.shared.model.core.SelfEvaluation;

/**
 * Class <strong>EditFactor2EvaluatePresenter</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class EditFactor2EvaluatePresenter implements EditPresenter {

	public interface EditFactor2EvaluateDisplay extends Display {

		String getName();

		void setName(String name);

		String getDescription();

		void setDescription(String description);

		float getWeighing();

		void setWeighing(float weighing);

		String getJustify();

		void setJustify(String justify);

		void setGrade(float grade);

		void show();

		void hide();
	}

	private final HandlerManager eventBus;
	private final CoreServiceAsync rpcService;
	private final EditFactor2EvaluateDisplay display;
	private Factor2Evaluate factor2Evaluate;

	public EditFactor2EvaluatePresenter(HandlerManager eventBus,
			CoreServiceAsync rpcService, EditFactor2EvaluateDisplay display,
			String factorId) {

		this.rpcService = rpcService;
		this.eventBus = eventBus;
		this.display = display;
		display.setPresenter(this);

		if (factorId == null) {
			factor2Evaluate = new Factor2Evaluate();

		} else {
			loadBean(factorId);
		}

	}

	private void loadBean(String factorId) {
		AppController.setStatusMessage("Loading...");
		rpcService.getFactor2Evaluate(factorId,
				new AsyncCallback<Factor2Evaluate>() {
					@Override
					public void onSuccess(Factor2Evaluate result) {
						if (result == null) {
							onFailure(new Throwable("Database error"));
							return;
						}
						factor2Evaluate = result;
						display.setName(factor2Evaluate.getName());
						display.setDescription(factor2Evaluate.getDescription());
						display.setWeighing(factor2Evaluate.getWeighing());
						display.setJustify(factor2Evaluate.getJustify());
						display.setGrade(factor2Evaluate.getGrade());
						hideStatusMessage();
					}

					@Override
					public void onFailure(Throwable caught) {
						GWT.log(Constants.MESSAGE_FAILED_GET
								+ Factor2Evaluate.class.getName(), caught);
						factor2Evaluate = new Factor2Evaluate();
						// TODO i18n
						String failedMessage = "Failed to retrieve factor2Evaluate";
						display.showInfo(failedMessage);
						hideStatusMessage();
					}

					void hideStatusMessage() {
						AppController.hideStatusMessage();
					}
				});
	}

	@Override
	public void doSaveOrUpdate() {
		factor2Evaluate.setName(display.getName());
		factor2Evaluate.setDescription(display.getDescription());
		factor2Evaluate.setWeighing(display.getWeighing());
		factor2Evaluate.setJustify(display.getJustify());
		String currentEvaluationId = (String) AppController
				.getAttribute(Constants.CURRENT_EVALUATION_ID);
		factor2Evaluate.setProcess(new SelfEvaluation(currentEvaluationId));

		// TODO i18n
		final String failedMessage = "Failed to save factor2Evaluate";
		final String successMessage = "Factor2Evaluate saved sucessfully";

		display.hide();
		AppController.setStatusMessage("Saving...");
		rpcService.saveOrUpdateFactor2Evaluate(factor2Evaluate,
				new AsyncCallback<Boolean>() {
					@Override
					public void onSuccess(Boolean result) {
						if (result) {
							eventBus.fireEvent(new ListFactors2EvaluateEvent());
							display.showInfo(successMessage);
							hideStatusMessage();

						} else {
							onFailure(new Throwable("Database error"));
						}
					}

					@Override
					public void onFailure(Throwable caught) {
						GWT.log(Constants.MESSAGE_FAILED_SAVE
								+ Factor2Evaluate.class.getName(), caught);
						display.showInfo(failedMessage);
						hideStatusMessage();
					}

					void hideStatusMessage() {
						AppController.hideStatusMessage();
					}
				});
	}

	@Override
	public void go(HasWidgets container) {
		display.show();
	}

}
